package pub.developers.forum.portal.controller.rest;

import org.springframework.web.bind.annotation.*;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.api.model.PageResponseModel;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.comment.CommentCreateRequest;
import pub.developers.forum.api.response.comment.CommentPageResponse;
import pub.developers.forum.api.response.config.ConfigResponse;
import pub.developers.forum.api.service.CommentApiService;
import pub.developers.forum.common.constant.Constant;
import pub.developers.forum.portal.support.WebUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/comment-rest")
public class CommentRestController {

    @Resource
    private CommentApiService commentApiService;


    @PostMapping("/create")
    public ResultModel create(@RequestBody CommentCreateRequest createRequest, HttpServletRequest request) throws Exception {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return commentApiService.create(createRequest);
    }

    @PostMapping("/beCommented")
    public ResultModel beCommented(HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return commentApiService.beCommented();
    }

    //给评论点赞
    /*
    谁给哪个评论点了赞
    需要当前登录用户的id 以及 被点赞的评论id
     */
    @PostMapping("/followComment/{commentID}")
    public ResultModel followComment(HttpServletRequest request , @PathVariable Long commentID)
    {
        //验证用户登录状态, 存储登录用户的信息
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY , WebUtil.cookieGetSid(request));
        return commentApiService.followCommentService(commentID);
    }

    //给评论取消点赞
    @PostMapping("unfollowComment/{commentID}")
    public ResultModel<List<ConfigResponse>>unfollowComment(HttpServletRequest request  , @PathVariable Long commentID)
    {
        //验证用户登录状态 , 存储用户登录信息
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY , WebUtil.cookieGetSid(request));
        return  commentApiService.unfollowCommentService(commentID);
    }

    //根据第一级的评论拿到二级评论
    @PostMapping("/allReply")
    public ResultModel<PageResponseModel<CommentPageResponse>> allReply(@RequestBody PageRequestModel pageRequestModel, HttpServletRequest httpServletRequest)
    {
        httpServletRequest.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY , WebUtil.cookieGetSid(httpServletRequest));
        return commentApiService.allReplyService(pageRequestModel);
    }
}
